package com.tcheung.utils;

import com.tcheung.MRR;
import com.tcheung.entity.Document;
import com.tcheung.entity.TFEntity;
import com.tcheung.entity.TFIDFEntity;
import com.tcheung.service.AnswerService;
import com.tcheung.service.QueryService;

import java.io.*;
import java.util.List;

/**
 * FileUtils
 *
 * @author: zhangteng
 * @time: 2014/11/30 11:47
 */
public class FileUtils {

    public static final String DOC_INDEX_FILE_NAME = "DocIndex.txt";

    public static final String DOC_INDEX_TF_FILE_NAME = "DocIndex(tf).txt";

    public static final String DOC_INDEX_TF_IDF_FILE_NAME = "DocIndex(tfidf).txt";

    public static final String DOC_INVERT_TF_FILE_NAME = "DocInvert(tf).txt";

    public static final String DOC_INVERT_TF_IDF_FILE_NAME = "DocInvert(tfidf).txt";

    public static final String RESULT_VECTORAL_FILE_NAME = "result(vectoral)_NUM.txt";

    /**
     * 写入docIndex文件
     *
     * @param path
     * @param documentList
     * @throws IOException
     */
    public static void writeToDocIndex(String path, List<Document> documentList) throws IOException {
        File file = new File(path + "/" + DOC_INDEX_FILE_NAME);

        Writer writer = new BufferedWriter(new FileWriter(file));

        for (Document document : documentList) {
            for (TFEntity tfEntity : document.getTfEntityList()) {
                writer.write(tfEntity.toString() + "\n");
            }
        }

        writer.close();
    }

    /**
     * 写入docIndexTf
     *
     * @param path
     * @param tfEntityList
     * @throws IOException
     */
    public static void writeToDocIndexTF(String path, List<TFEntity> tfEntityList) throws IOException {
        File file = new File(path + "/" + DOC_INDEX_TF_FILE_NAME);

        Writer writer = new BufferedWriter(new FileWriter(file));

        for (TFEntity tfEntity : tfEntityList) {
            writer.write(tfEntity.toString1() + "\n");
        }

        writer.close();
    }

    /**
     * 写入DocIndexTFIDF
     *
     * @param path
     * @param tfidfEntityList
     * @throws IOException
     */
    public static void writeToDocIndexTFIDF(String path, List<TFIDFEntity> tfidfEntityList) throws IOException {
        File file = new File(path + "/" + DOC_INDEX_TF_IDF_FILE_NAME);

        Writer writer = new BufferedWriter(new FileWriter(file));

        for (TFIDFEntity tfidfEntity : tfidfEntityList) {
            writer.write(tfidfEntity.toString() + "\n");
        }

        writer.close();
    }

    /**
     * 写入DocInvertTF
     *
     * @param path
     * @param tfEntityList
     * @throws IOException
     */
    public static void writeToDocInvertTF(String path, List<TFEntity> tfEntityList) throws IOException {
        File file = new File(path + "/" + DOC_INVERT_TF_FILE_NAME);

        Writer writer = new BufferedWriter(new FileWriter(file));

        for (TFEntity tfEntity : tfEntityList) {
            writer.write(tfEntity.toString2() + "\n");
        }

        writer.close();
    }

    /**
     * 写入DocInvertTFIDF
     *
     * @param path
     * @param tfidfEntityList
     * @throws IOException
     */
    public static void writeToDocInvertTFIDF(String path, List<TFIDFEntity> tfidfEntityList) throws IOException {
        File file = new File(path + "/" + DOC_INVERT_TF_IDF_FILE_NAME);

        Writer writer = new BufferedWriter(new FileWriter(file));

        for (TFIDFEntity tfidfEntity : tfidfEntityList) {
            writer.write(tfidfEntity.toString1() + "\n");
        }

        writer.close();
    }

    /**
     * 写入查询结果
     *
     * @param path
     * @param documentList
     * @param queryNum
     * @param query
     * @throws IOException
     */
    public static void writeToResult(String path, List<Document> documentList, int queryNum, String query) throws IOException {
        String filename = RESULT_VECTORAL_FILE_NAME;
        filename = filename.replaceAll("NUM", String.valueOf(queryNum));

        File file = new File(path + "/" + filename);

        BufferedWriter writer = new BufferedWriter(new FileWriter(file));

        writer.write("Request " + queryNum + ": " + query + "\n");
        for (Document document : documentList) {
            writer.write(document.getDocNum() + "\t" + String.format("%.4f", document.getSimilarity()) + "\t" + document.getContent(20) + "\n");
        }

        writer.close();
    }

    /**
     * 写入结果统计文件
     *
     * @param path
     * @throws Exception
     */
    public static void writeToResultStatistics(String path) throws Exception {
        File file = new File(path + "/result_statistics.txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));

        writer.write("queryNum"+"\t"+"queryAnswer" + "\t" + "rightAnswer" + "right/wrong" + "\n");

        QueryService queryService = QueryService.getInstance();
        AnswerService answerService = AnswerService.getInstance();

        int queryAnswer, rightAnswer, rightCount = 0;
        for (Integer queryNum : queryService.getQueries().keySet()) {
            // 去查询结果集中的第一条
            queryAnswer = queryService.getAnswer(queryNum).get(0);
            rightAnswer = answerService.getAnswerByQueryNum(queryNum);
            writer.write(queryNum + "\t" +
                         queryAnswer + "\t" +
                         rightAnswer + "\t" +
                    (queryAnswer == rightAnswer ? "right" : "wrong") + "\n");
            if (rightAnswer == queryAnswer) {
                rightCount ++;
            }
        }

        writer.write("\n");
        writer.write("rights: " + rightCount);

        writer.close();
    }

    /**
     * 写入mrr文件
     *
     * @param path
     * @throws Exception
     */
    public static void writeToMRR(String path) throws Exception {
        File file = new File(path + "/mrr.txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));

        MRR mrr = MRR.getInstance();
        writer.write(mrr.getMRRStr());

        writer.close();
    }
}
